package net.zjitc.service.impl;

import net.zjitc.dao.CartDao;
import net.zjitc.dao.OrderDao;
import net.zjitc.dao.impl.CartDaoImpl;
import net.zjitc.dao.impl.OrderDaoImpl;
import net.zjitc.pojp.Order;
import net.zjitc.service.CartService;
import net.zjitc.service.OrderService;
import net.zjitc.utils.db.C3P0Utils;

import java.sql.SQLException;


/**
 * Created by Administrator on 2017/12/14.
 */
public class OrderServiceImpl implements OrderService {
    OrderDao dao=new OrderDaoImpl();
    CartDao cartDao=new CartDaoImpl();
    public int add(Order order) {
        /**
         * 插入一条订单（订单，订单条目） 事务
         * 开始事务C3P0
         * 执行sql
         * 提交事务
         * 回滚
         */
        try {
            C3P0Utils.startTransation();//th

            /**
             *   购物车清空 与 订单生成 看成一个业务
             要同时操作成功才可以，只要一个没成功，则回滚
             */
            dao.add(order);
            cartDao.deleteByUid(order.getUser().getUid());

            C3P0Utils.commitTransation();
        } catch (SQLException e) {
            try {
                C3P0Utils.rollbackTransation();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        return 0;
    }
}
